<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\ProductCate;
use app\common\model\ProductSpecData;
use app\common\model\Spec;
use app\common\model\SpecItem;
use app\common\tool\Result;
use think\Response;

class BusinessSpec extends Base
{

    /**
     * @param $page
     * @return Response
     * 原点 2022年12月24日09:20:46 规格列表
     */
    public function index(): Response
    {
        $map = ['is_del' => 0, 'business_id' => $this->business->id];
        $list = Spec::getPageList($map, 'id,cate_1,cate_2,cate_3,key,value,create_time', $this->getPage(),
            $this->getLimit(), false, ['cate1', 'cate2', 'cate3']);
        return Result::fastReturn($list);
    }

    /**
     * @param $id
     * @return Response
     * 原点 2022年12月24日09:55:54 规格详情
     */
    public function read($id): Response
    {
        try {
            $info = Spec::with('item')
                ->where(['id' => $id, 'is_del' => 0, 'business_id' => $this->business->id])
                ->field('id,cate_1,cate_2,cate_3,key,value')->find();
            if (empty($info)) {
                return Result::fail('规格不存在');
            }
            return Result::success('success', $info);
        } catch (\Exception $e) {
            return Result::fail('服务器错误！');
        }
    }

    /**
     * @return Response
     * 原点 2022年12月24日09:36:59 修改或新增规格
     */
    public function save(): Response
    {
        try {
            $param = $this->request->post();
            $this->validate($param, 'Spec');
            $param['business_id'] = $this->business->id;
            if (isset($param['item_id']) && $param['item_id']) {
                $param['item_id'] = explode(',', $param['item_id']);
            }
            $param['value'] = explode(',', $param['value']);
            return Result::fastReturn(Spec::_save($param, ''));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * @return Response
     * 原点 2022年12月30日10:24:10 删除规格
     */
    public function delete($id): Response
    {
        try {
            $info = Spec::where(['business_id' => $this->business->id, 'id' => $id])->find();
            if (empty($info)) {
                return Result::fail('未查询到此规格');
            }
            //查询规格是否在使用
            $count = ProductSpecData::where('spec_id', $id)->count();
            if ($count) {
                return Result::fail('此规格已有商品使用，无法删除');
            }
            $info->delete();
            SpecItem::destroy(['spec_id' => $id]);
            return Result::success('已删除');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * @return Response
     * 原点 2022年12月24日15:24:31 组合sku数据
     */
    public function createSku(): Response
    {
        try {
            $array = $this->request->post('sku');
            $data = [];
            foreach ($array as $k => $v) {
                $itemArray = [];
                foreach ($v['item'] as $item) {
                    $itemArray[] = [
                        'id'   => $item['id'],
                        'name' => $item['name']
                    ];
                }
                $data[] = $itemArray;
            }
            $len = count($data);
            $result = $data[0];
            for ($i = 0; $i < $len - 1; $i++) {
                $arr1 = $result;
                $result = [];
                foreach ($arr1 as $res) {
                    foreach ($data[$i + 1] as $sec) {
                        if (!is_array($res)) {
                            $res = array($res);
                        }
                        if (!is_array($sec)) {
                            $sec = array($sec);
                        }
                        $result[] = array_merge_recursive($res, $sec);
                    }
                }
            }
            $sku = [];

            foreach ($result as $k => $item) {
                if (is_array($item['id'])) {
                    $sku[] = [
                        'sku_id'   => implode('-', $item['id']),
                        'sku_item' => implode('/', $item['name'])
                    ];
                } else {
                    $sku[] = [
                        'sku_id'   => $item['id'],
                        'sku_item' => $item['name']
                    ];
                }
            }
            return Result::success('success', $sku);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }


}

